﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FunctionalCLR.Core;
using Zove.Desktop.Tools;

namespace Zove.Desktop.Function {
    public class PieChartNode : FuncNode {

        public PieChartNode()
            : base() {
            this.Value = "PieChart";
        }

        public override object Evaluate() {
            if (Children.Count < 3) {
                throw new ArgumentException("Usage: PieChart(title, [], []);");
            }

            string title = Children[Children.Count - 1].Evaluate().ToString();
            Node p = Children[Children.Count - 2];
            if (!(p is ListNode)) {
                throw new ArgumentException("Parameters should be a List");
            }

            Node q = Children[Children.Count - 3];
            if (!(q is ListNode)) {
                throw new ArgumentException("Values should be a List");
            }

            ListNode names = Children[Children.Count - 2] as ListNode;
            List<string> n = new List<string>();
            foreach (Node child in names.Children) {
                n.Add(child.Evaluate().ToString());
            }

            ListNode values = Children[Children.Count - 3] as ListNode;
            List<double> m = new List<double>();
            foreach (Node child in values.Children) {
                object value = child.Evaluate();
                m.Add(Convert.ToDouble(value));
            }

            PieChartWindow pieChartWindow = new PieChartWindow(
                title, n.ToArray(), m.ToArray(), "");
            pieChartWindow.MdiParent = DesktopFormContext.Context.Main;
            pieChartWindow.Show();

            return string.Empty;
        }
    }
}
